$.detail = function () {
    return {
        assessment:'',
        init:function () {
            this.eventListen();
            this.ajaxRequest('/feedback/feedbackDetail','post',{feedbackId:$.detail.getQueryVariable('id'),userId:$.detail.getQueryVariable('userId')},false,function (res) {
                $.detail.render(res);
            });
            if(this.getQueryVariable('userType') === 'student'){
                $('.line').eq(1).hide();
                $('.submit').hide();
            }
        },
        getQueryVariable: function(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] === variable){return pair[1];}
            }
            return(false);
        },
        render:function (res) {
            var template = '';
            if(res.replyStatus === 1){
                $('.replyStatusSpan').html('已回复');
                $.each(res.replyContent,function (i,v) {
                    template += '<div class="replyContent '+ (i===0 ? "one" :"") +' ">' +
                        '            <div class="replyTime">' +
                        '                <span>平台回复：</span>' +
                        '                <span>'+ v.createTime +'</span>' +
                        '            </div>' +
                        '            <div class="replyText">' +
                        '                <p>'+ v.content +'</p>' +
                        '            </div>' +
                        '        </div>'
                });
                $('.replyStatus').html(template);
            }else{
                $('.replyStatus').hide();
                $('.replyStatusSpan').html('未回复');
            }
            switch (res.assessment){
                case 0:
                    $('.submit').show();
                    break;
                case 1:
                    $('.detailTopBox .line .ass span').eq(0).addClass('green');
                    $('.detailTopBox .line .ass span').eq(1).hide();
                    $('.submit').hide();
                    break;
                case 2:
                    $('.detailTopBox .line .ass span').eq(0).hide();
                    $('.detailTopBox .line .ass span').eq(1).addClass('red');
                    $('.submit').hide();
                    break;
            }
            $('.createTime').html(res.createTime);
            $('.appName').html(res.appName);
            $('.feedbackType').html(res.feedbackType);
            $('.describe').html(res.describe);
            if(res.image.length > 0){
                $.each(res.image,function (i,v) {
                    $('.allLine.image .img').append('<img src="'+ v +'">')
                    $('.banner ul').append('<li><img src="'+ v +'" alt=""></li>')
                });
                $('.allLine.image .img img').height($('.allLine.image .img img').width());
            }else{
                $('.allLine.image').remove();
            }
        },
        eventListen:function () {
            //提交数据
            $('.submit').click(function () {
                if(!$.detail.assessment){
                    layui.layer.msg('请先评价',{icon:2});
                    return;
                }
                var formdata = new FormData();
                formdata.append('feedbackId',$.detail.getQueryVariable('id'))
                formdata.append('assessment',$.detail.assessment);

                $.detail.ajaxRequest('/feedback/assessFeedback','post',{feedbackId:$.detail.getQueryVariable('id'),assessment:$.detail.assessment},false,function (res) {
                    if(res.code){
                        layui.layer.msg(res.msg,{icon:1});
                        setTimeout(function () {
                            location.reload();
                        },1000)
                    }else{
                        layui.layer.msg(res.msg,{icon:2});
                    }
                });
            });

            //选择满意度
            $('.detailTopBox .line .ass span').click(function () {
                var index = $(this).index();
                $('.detailTopBox .line .ass span').removeClass();
                switch (index){
                    case 0:
                        $(this).addClass('green');
                        $.detail.assessment = 1;
                        break;
                    case 1:
                        $(this).addClass('red');
                        $.detail.assessment = 2;
                        break;
                }
            });

            //弹出banner
            $('.allLine.image .img').on('click','img',function () {
                var index = $(this).index();
                $('.banner').fadeIn();
                $('.banner ul li img').each(function () {
                    $(this).css({
                        'margin-left': -$(this).width()/2 + 'px',
                        'margin-top':-$(this).height()/2 + 'px'
                    })
                });
                $.detail.bannerInit(index);
            });

            //关闭banner
            $('.banner .title .back').click(function () {
                $(this).parents('.banner').fadeOut();
            })
        },
        bannerInit:function (index) {
            var i = index;
            var oLi = $('.banner ul').find('li');
            $('.banner .title .text').html( (i+1) +' / ' + oLi.length);
            $(this).removeClass('transition');
            $('.banner ul').css({'transform':'translateX('+ -i*oLi.eq(0).width() +'px) translateZ(0)'});
            $('.banner').on('touchstart','ul',function (e) {
                var tX = $(this).css('transform').split(',')[4] ? parseFloat($(this).css('transform').split(',')[4]) : 0;
                var sX = e.originalEvent.changedTouches[0].pageX,cX,eX;
                $(this).removeClass('transition');
                $(this).on('touchmove',function (e) {
                    eX = e.originalEvent.changedTouches[0].pageX -sX;
                    cX = tX + eX;
                    $(this).css({
                        'transform':'translateX('+ cX +'px) translateZ(0)'
                    })
                });
                $(this).on('touchend',function (e) {
                    if(Math.abs(eX) > oLi.eq(0).width()/6){
                        if(eX < 0){
                            i++;
                            if(i>oLi.length-1) i--;
                        }else{
                            i--;
                            if(i < 0) i++;
                        }
                        $(this).addClass('transition')
                    }
                    $(this).css({
                        'transform':'translateX('+ -i*oLi.eq(0).width() +'px) translateZ(0)'
                    });
                    $('.banner .title .text').html( (i+1) +' / ' + oLi.length);
                    $(this).off('touchmove');
                    $(this).off('touchend');
                    return false;
                });

            })
        },
        ajaxRequest: function (url,type,data,sync,callback) {
            $.ajax({
                url: url,
                type:type,
                data:data,
                async: sync,
                dataType: "json",
                complete:function (XMLHttpRequest, textStatus) {
                    if(XMLHttpRequest.status===200){
                        if(XMLHttpRequest.responseJSON){
                            callback&&callback(XMLHttpRequest.responseJSON);
                        }else{
                            callback&&callback(JSON.parse(XMLHttpRequest.responseText));
                        }
                    }else{
                        layui.use(['layer'],function () {
                            layui.layer.msg(XMLHttpRequest.responseJSON.error,{icon:2})
                        });
                    }
                }
            })
        }
    }
}();
$.detail.init();